package org.sundatagroup.aurora.monitor.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.gson.JsonElement;

/**
 * <p>用于同步从展示区发来的展示屏更新数据
 * </p>
 * 
 * @author Zdream
 * @date 2018-05-23
 * @since 0.2.0
 */
@Service
public class StatusUpdater implements IStatusUpdateAware {

	private final static Logger LOGGER = LoggerFactory.getLogger(StatusUpdater.class);
	
    @Autowired
    MonitorService monitors;
    
	@Override
	public void statusUpdate(String sid) {
    	monitors.updateScreenStatus(sid);
	}
	
	@Override
	public void onScreenCreate(String sid) {
		LOGGER.info("监控区侦测到屏幕 #" + sid + " 的创建");
		monitors.addScreen(sid);
	}
	
	@Override
	public void onScreenDestroy(String sid) {
		LOGGER.info("监控区侦测到屏幕 #" + sid + " 的销毁");
		monitors.removeScreen(sid);
	}

	@Override
	public void recieve(String msg, JsonElement data) throws Exception {
		switch (msg) {
		case "show-screen-status-update": {
			statusUpdate(data.getAsString());
		} break;
		case "show-screen-status-create": {
			this.onScreenCreate(data.getAsString());
		} break;
		case "show-screen-status-close": {
			this.onScreenDestroy(data.getAsString());
		} break;

		default:
			break;
		}
	}
	
}
